/*
 * Copyright (C) 2008  Wang Hoi <fearee@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  US
*/
#include "filecache.h"
#include <QtCore>
using namespace FileCachePrivate;
FileCache::FileCache(int threads)
{
    if (threads == -1)
        threads = QThread::idealThreadCount();
    pool.setMaxThreadCount(threads);
}
void FileCache::get(const QString &key)
{
    GetJob *job = new GetJob(key);
    connect(job, SIGNAL(done(const QString&, const QByteArray&)),
            SIGNAL(valueGot(const QString&, const QByteArray&)));
    connect(job, SIGNAL(errorGot(const QString&)),
            SIGNAL(errorGot(const QString&)));
    pool.start(job);
}

void FileCache::set(const QString &key, const QByteArray &value)
{
    SetJob *job = new SetJob(key, value);
    connect(job, SIGNAL(done(const QString&, const QByteArray&)),
            SIGNAL(valueSet(const QString&, const QByteArray&)));
    connect(job, SIGNAL(errorGot(const QString&)),
            SIGNAL(errorGot(const QString&)));
    pool.start(job);
}
void FileCache::del(const QString &key)
{
    DelJob *job = new DelJob(key);
    connect(job, SIGNAL(done(const QString&)),
            SIGNAL(valueDel(const QString&)));
    connect(job, SIGNAL(errorGot(const QString&)),
            SIGNAL(errorGot(const QString&)));
    pool.start(job);
}
GetJob::GetJob(const QString &key)
{
    this->key = key;
}

SetJob::SetJob(const QString &key, const QByteArray &value)
{
    this->key = key;
    this->value = value;
}

DelJob::DelJob(const QString &key)
{
    this->key = key;
}

void GetJob::run()
{
    QFile file(key);
    QByteArray buf;
    if (file.open(QIODevice::ReadOnly)) {
        buf = file.readAll();
        if (file.error() == QFile::NoError) {
            file.close();
            qDebug() << "File GetJob:" << key << buf.size();
            emit done(key, buf);
            return;
        }
    }
    file.close();
    emit errorGot(QString("Get Failed--%1--%2").arg(key).arg(file.errorString()));
}

void SetJob::run()
{
    QFile file(key);
    int len;
    if (file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
        len = file.write(value);
        if (len == value.size()) {
            file.close();
            emit done(key, value);
            return;
        }
    }
    emit errorGot(QString("Set Failed--%1--%2").arg(key).arg(file.errorString()));
}

void DelJob::run()
{
    QFile file(key);
    if (!file.exists() || file.remove()) {
        emit done(key);
        return;
    }
    emit errorGot(QString("Del Failed--%1--%2").arg(key).arg(file.errorString()));
}

#include "filecache.moc"
// vim: sw=4 sts=4 et tw=100
